package com.test.service.impl;

import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
import com.test.entity.UserBorrowView;
import com.test.feign.BookFeignClient;
import com.test.feign.UserFeignClient;
import com.test.mapper.BorrowMapper;
import com.test.service.BorrowService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    private BorrowMapper borrowMapper;
    @Resource
    private BookFeignClient bookFeignClient;
    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public UserBorrowView getBorrowViewByUid(int uid) {
        List<Borrow> borrowList = borrowMapper.getBorrowsByUid(uid);
        User user = userFeignClient.findUserById(uid);
        List<Book> bookList = new ArrayList<>();
        borrowList.forEach(o -> bookList.add(bookFeignClient.getBookById(o.getBid())));
        return new UserBorrowView(user, bookList);
    }
}
